import time
from pickletools import long1
from time import sleep

from Tools.scripts.cleanfuture import dryrun
from selenium.webdriver.common.by import By

from common.com import forum, Driver


#登录页面的测试
class forumlogin:
    #创一个字符串用存储url
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://127.0.0.1:9580/sign-in.html"
        self.driver = forum.driver
        self.driver.get(self.url)

    #正常登录的测试用例
    def normalTest(self):
        #首先清除原本输入框的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        #寻找用户名和密码输入框输入正确的用户名和密码
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("Test1")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        #点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        #验证是否正常登录通过寻找论坛首页的中的用户名进行验证
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a > div")
        #首先最大化窗口进行屏幕截图
        self.driver.maximize_window()
        forum.getscreenshot()
        #因为需要测试异常登录所以就需要先让界面回到登录界面
        # self.driver.back()
        # time.sleep(3)
        # self.driver.quit()
    #异常登录
    def abnormalTest(self):
        #测试用例一:输入错误的用户名和错误的密码
        #首先清除原本输入框的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        time.sleep(1)
        # 寻找用户名和密码输入框输入错误的的用户名和密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("Test11")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        time.sleep(1)
        #点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #首先最大化窗口进行屏幕截图
        self.driver.maximize_window()
        forum.getscreenshot()
        time.sleep(1)
        #输入错误的用户名和密码时会显示用户名或密码错误需要验证是否异常登录
        adim = self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert adim == "警告"
        time.sleep(1)
#############################################################
        #测试用例二：输入错误用户名和正确的密码
        #首先清除原本输入框的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        time.sleep(1)
        # 寻找用户名和密码输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("Test11")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        time.sleep(1)
        #点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #首先最大化窗口进行屏幕截图
        self.driver.maximize_window()
        forum.getscreenshot()
        time.sleep(1)
        #输入错误的用户名和密码时会显示用户名或密码错误需要验证是否异常登录
        adim = self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert adim == "警告"
        time.sleep(1)
####################################################
        #测试用例三：输入正确的用户名和错误的密码
        #首先清除原本输入框的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        time.sleep(1)
        # 寻找用户名和密码输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("Test1")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        time.sleep(1)
        #点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #首先最大化窗口进行屏幕截图
        self.driver.maximize_window()
        forum.getscreenshot()
        time.sleep(1)
        #输入错误的用户名和密码时会显示用户名或密码错误需要验证是否异常登录
        adim = self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert adim == "警告"
        time.sleep(1)
####################################################
        # 测试用例四：不输入用户名但输入密码
        #首先清除原本输入框的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        time.sleep(1)
        # 密码输入框

        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        time.sleep(1)
        #点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #首先最大化窗口进行屏幕截图
        self.driver.maximize_window()
        forum.getscreenshot()
        time.sleep(1)
        #输入错误的用户名和密码时会显示用户名或密码错误需要验证是否异常登录
        adim = self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-3 > div").text
        assert adim == "用户名不能为空"
        time.sleep(1)
####################################################
        # 测试用例五：输入用户名但不输入密码
        # 首先清除原本输入框的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        time.sleep(1)
        # 寻找用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("Test1")

        time.sleep(1)
        #点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #首先最大化窗口进行屏幕截图
        self.driver.maximize_window()
        forum.getscreenshot()
        time.sleep(1)
        #输入错误的用户名和密码时会显示用户名或密码错误需要验证是否异常登录
        adim = self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-2 > div > div").text
        assert adim == "密码不能为空"
        time.sleep(1)


#测试
login = forumlogin()
# login.abnormalTest()
login.normalTest()



